
const { dbMgr } = require('../config')
const omit = require('lodash/omit')


exports.main = async (event, context) => {
  const { collectionName, condition } = event;
  if (!collectionName) {
    return '缺少集合名称'
  }


  const otherCondition = omit(condition, ['assign', 'isNewAdd'])
  const assign = condition.assign
  const isNewAdd = condition.isNewAdd
  const grid = condition.grid

  let _condi = {}
  if (assign) {
    _condi['staff'] = assign === '1' ? { $ne: '' } : { $eq: '' }
    grid && (_condi.grid = assign === '2' ? { $in: [grid, ''] } : { $eq: grid })
  }

  Object.keys(otherCondition).forEach(key => {
    otherCondition[key] && (_condi[key] = { $regex: new RegExp(otherCondition[key], 'i').toString() })
  })

  if (condition.hasOwnProperty('isNewAdd')) {
    if (condition.isNewAdd == "是") {
      _condi.isNewAdd = condition.isNewAdd;
    } else {
      _condi.isNewAdd = { $ne: '是' };
    }
  }
  console.log(_condi)
  // 获取数据库实例ID
  const { EnvInfo } = await dbMgr.env.getEnvInfo()

  const { Databases } = EnvInfo

  const { Data } = await dbMgr.commonService('flexdb').call({
    Action: 'Query',
    Param: {
      TableName: collectionName,
      MgoQuery: JSON.stringify(_condi),
      Tag: Databases[0].InstanceId,
      MgoLimit: 100
    }
  })
  console.log('Data:', Data)
}
